﻿using Module_Debug.Hardware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class FT2306_Gamma : Form
    {
        #region 参数定义
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();
        //Class_JOJO_gamma Flexible = new Class_JOJO_gamma();
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private static bool Gamma_adjust_timeout_flag = false;

        private ushort[] gamma_r = new ushort[25];
        private ushort[] gamma_g = new ushort[25];
        private ushort[] gamma_b = new ushort[25];

        private byte[] table_c = new byte[25] { 0, 1, 3, 5, 7, 11, 15, 23, 31, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 207, 223, 239, 247, 255 };
        private byte[] table_s = new byte[25] { 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58 };
        //Gamma绑点定义
        private int Total_points = 0;
        //NT38360 监听挂载
        frmMain frmHelp = null;
        public FT2306_Gamma(frmMain NT38360)
        {
            frmHelp = NT38360;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体       
        }
        //NT38360 监听卸载
        private void NT38360_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(NT38360_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.Child_window_hide_already = false;
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT38360_Gamma_FormClosed！\r\n";
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void NT38360_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(NT38360_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT38360_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(NT38360_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT38360_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }

        //串口接收
        private void NT38360_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    // 0x34: NT38360的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x34 && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        //---------------------------------------------------NT38360---------------------------------------
                        if (Rec_tmp[0] == 0x34)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                case 0x02:
                                    {
                                        int i;
                                        string tmp = "";
                                        for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);

                                        NT38360_Reg_Data.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "寄存器\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x01) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x07:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x06:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma 绑点重置 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma4 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        NT38360_Time.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + NT38360_Time.Text + "次\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写第" + NT38360_Time.Text + "次\t写 OK！\r\n";
                                    }
                                    break;
                                case 0x20:
                                    {
                                        if (Rec_tmp[4] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change Gray\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Change GrayP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x1C:
                                    {
                                        if (Rec_tmp[3] == 0x00) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02) frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Setting.TempRecord.FT2306CmdType == 0x03) //red
                                        {
                                            NT38360_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT38360_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT38360_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT38360_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT38360_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT38360_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT38360_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT38360_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            NT38360_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT38360_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT38360_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT38360_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT38360_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT38360_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT38360_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            // if (start_15gamma == false)
                                            // {
                                            NT38360_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            NT38360_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT38360_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT38360_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT38360_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT38360_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT38360_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT38360_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT38360_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            NT38360_R24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            // }
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.FT2306CmdType == 0x04) //green
                                        {
                                            NT38360_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT38360_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT38360_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT38360_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT38360_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT38360_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT38360_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT38360_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            NT38360_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT38360_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT38360_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT38360_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT38360_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT38360_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT38360_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            // if (start_15gamma == false)
                                            // {
                                            NT38360_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            NT38360_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT38360_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT38360_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT38360_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT38360_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT38360_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT38360_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT38360_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            NT38360_G24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            // }

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.FT2306CmdType == 0x05) //blue
                                        {
                                            NT38360_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            NT38360_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            NT38360_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            NT38360_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            NT38360_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            NT38360_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            NT38360_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            NT38360_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            NT38360_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            NT38360_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            NT38360_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            NT38360_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            NT38360_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            NT38360_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            NT38360_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            // if (start_15gamma == false)
                                            // {
                                            NT38360_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            NT38360_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            NT38360_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            NT38360_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            NT38360_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            NT38360_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            NT38360_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            NT38360_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            NT38360_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            NT38360_B24.Value = Decimal.Parse((Rec_tmp[52] & 0x0f).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            // }

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                default: break;
                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }
        //Gamma保存
        private void NT38360_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + NT38360_R0.Text + "," + "G0," + NT38360_G0.Text + "," + "B0," + NT38360_B0.Text + "\r\n";
                    str = str + "R1," + NT38360_R1.Text + "," + "G1," + NT38360_G1.Text + "," + "B1," + NT38360_B1.Text + "\r\n";
                    str = str + "R2," + NT38360_R2.Text + "," + "G2," + NT38360_G2.Text + "," + "B2," + NT38360_B2.Text + "\r\n";
                    str = str + "R3," + NT38360_R3.Text + "," + "G3," + NT38360_G3.Text + "," + "B3," + NT38360_B3.Text + "\r\n";
                    str = str + "R4," + NT38360_R4.Text + "," + "G4," + NT38360_G4.Text + "," + "B4," + NT38360_B4.Text + "\r\n";
                    str = str + "R5," + NT38360_R5.Text + "," + "G5," + NT38360_G5.Text + "," + "B5," + NT38360_B5.Text + "\r\n";
                    str = str + "R6," + NT38360_R6.Text + "," + "G6," + NT38360_G6.Text + "," + "B6," + NT38360_B6.Text + "\r\n";
                    str = str + "R7," + NT38360_R7.Text + "," + "G7," + NT38360_G7.Text + "," + "B7," + NT38360_B7.Text + "\r\n";
                    str = str + "R8," + NT38360_R8.Text + "," + "G8," + NT38360_G8.Text + "," + "B8," + NT38360_B8.Text + "\r\n";
                    str = str + "R9," + NT38360_R9.Text + "," + "G9," + NT38360_G9.Text + "," + "B9," + NT38360_B9.Text + "\r\n";
                    str = str + "R10," + NT38360_R10.Text + "," + "G10," + NT38360_G10.Text + "," + "B10," + NT38360_B10.Text + "\r\n";
                    str = str + "R11," + NT38360_R11.Text + "," + "G11," + NT38360_G11.Text + "," + "B11," + NT38360_B11.Text + "\r\n";
                    str = str + "R12," + NT38360_R12.Text + "," + "G12," + NT38360_G12.Text + "," + "B12," + NT38360_B12.Text + "\r\n";
                    str = str + "R13," + NT38360_R13.Text + "," + "G13," + NT38360_G13.Text + "," + "B13," + NT38360_B13.Text + "\r\n";
                    str = str + "R14," + NT38360_R14.Text + "," + "G14," + NT38360_G14.Text + "," + "B14," + NT38360_B14.Text + "\r\n";
                    str = str + "R15," + NT38360_R15.Text + "," + "G15," + NT38360_G15.Text + "," + "B15," + NT38360_B15.Text + "\r\n";
                    str = str + "R16," + NT38360_R16.Text + "," + "G16," + NT38360_G16.Text + "," + "B16," + NT38360_B16.Text + "\r\n";
                    str = str + "R17," + NT38360_R17.Text + "," + "G17," + NT38360_G17.Text + "," + "B17," + NT38360_B17.Text + "\r\n";
                    str = str + "R18," + NT38360_R18.Text + "," + "G18," + NT38360_G18.Text + "," + "B18," + NT38360_B18.Text + "\r\n";
                    str = str + "R19," + NT38360_R19.Text + "," + "G19," + NT38360_G19.Text + "," + "B19," + NT38360_B19.Text + "\r\n";
                    str = str + "R20," + NT38360_R20.Text + "," + "G20," + NT38360_G20.Text + "," + "B20," + NT38360_B20.Text + "\r\n";
                    str = str + "R21," + NT38360_R21.Text + "," + "G21," + NT38360_G21.Text + "," + "B21," + NT38360_B21.Text + "\r\n";
                    str = str + "R22," + NT38360_R22.Text + "," + "G22," + NT38360_G22.Text + "," + "B22," + NT38360_B22.Text + "\r\n";
                    str = str + "R23," + NT38360_R23.Text + "," + "G23," + NT38360_G23.Text + "," + "B23," + NT38360_B23.Text + "\r\n";
                    str = str + "R24," + NT38360_R24.Text + "," + "G24," + NT38360_G24.Text + "," + "B24," + NT38360_B24.Text + "\r\n";

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void NT38360_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                NT38360_R0.Text = arrtmp[1].ToString(); NT38360_G0.Text = arrtmp[3].ToString(); NT38360_B0.Text = arrtmp[5].ToString();
                NT38360_R1.Text = arrtmp[7].ToString(); NT38360_G1.Text = arrtmp[9].ToString(); NT38360_B1.Text = arrtmp[11].ToString();
                NT38360_R2.Text = arrtmp[13].ToString(); NT38360_G2.Text = arrtmp[15].ToString(); NT38360_B2.Text = arrtmp[17].ToString();
                NT38360_R3.Text = arrtmp[19].ToString(); NT38360_G3.Text = arrtmp[21].ToString(); NT38360_B3.Text = arrtmp[23].ToString();
                NT38360_R4.Text = arrtmp[25].ToString(); NT38360_G4.Text = arrtmp[27].ToString(); NT38360_B4.Text = arrtmp[29].ToString();
                NT38360_R5.Text = arrtmp[31].ToString(); NT38360_G5.Text = arrtmp[33].ToString(); NT38360_B5.Text = arrtmp[35].ToString();
                NT38360_R6.Text = arrtmp[37].ToString(); NT38360_G6.Text = arrtmp[39].ToString(); NT38360_B6.Text = arrtmp[41].ToString();
                NT38360_R7.Text = arrtmp[43].ToString(); NT38360_G7.Text = arrtmp[45].ToString(); NT38360_B7.Text = arrtmp[47].ToString();
                NT38360_R8.Text = arrtmp[49].ToString(); NT38360_G8.Text = arrtmp[51].ToString(); NT38360_B8.Text = arrtmp[53].ToString();
                NT38360_R9.Text = arrtmp[55].ToString(); NT38360_G9.Text = arrtmp[57].ToString(); NT38360_B9.Text = arrtmp[59].ToString();
                NT38360_R10.Text = arrtmp[61].ToString(); NT38360_G10.Text = arrtmp[63].ToString(); NT38360_B10.Text = arrtmp[65].ToString();
                NT38360_R11.Text = arrtmp[67].ToString(); NT38360_G11.Text = arrtmp[69].ToString(); NT38360_B11.Text = arrtmp[71].ToString();
                NT38360_R12.Text = arrtmp[73].ToString(); NT38360_G12.Text = arrtmp[75].ToString(); NT38360_B12.Text = arrtmp[77].ToString();
                NT38360_R13.Text = arrtmp[79].ToString(); NT38360_G13.Text = arrtmp[81].ToString(); NT38360_B13.Text = arrtmp[83].ToString();
                NT38360_R14.Text = arrtmp[85].ToString(); NT38360_G14.Text = arrtmp[87].ToString(); NT38360_B14.Text = arrtmp[89].ToString();
                NT38360_R15.Text = arrtmp[91].ToString(); NT38360_G15.Text = arrtmp[93].ToString(); NT38360_B15.Text = arrtmp[95].ToString();
                NT38360_R16.Text = arrtmp[97].ToString(); NT38360_G16.Text = arrtmp[99].ToString(); NT38360_B16.Text = arrtmp[101].ToString();
                NT38360_R17.Text = arrtmp[103].ToString(); NT38360_G17.Text = arrtmp[105].ToString(); NT38360_B17.Text = arrtmp[107].ToString();
                NT38360_R18.Text = arrtmp[109].ToString(); NT38360_G18.Text = arrtmp[111].ToString(); NT38360_B18.Text = arrtmp[113].ToString();
                NT38360_R19.Text = arrtmp[115].ToString(); NT38360_G19.Text = arrtmp[117].ToString(); NT38360_B19.Text = arrtmp[119].ToString();
                NT38360_R20.Text = arrtmp[121].ToString(); NT38360_G20.Text = arrtmp[123].ToString(); NT38360_B20.Text = arrtmp[125].ToString();
                NT38360_R21.Text = arrtmp[127].ToString(); NT38360_G21.Text = arrtmp[129].ToString(); NT38360_B21.Text = arrtmp[131].ToString();
                NT38360_R22.Text = arrtmp[133].ToString(); NT38360_G22.Text = arrtmp[135].ToString(); NT38360_B22.Text = arrtmp[137].ToString();
                NT38360_R23.Text = arrtmp[139].ToString(); NT38360_G23.Text = arrtmp[141].ToString(); NT38360_B23.Text = arrtmp[143].ToString();
                NT38360_R24.Text = arrtmp[145].ToString(); NT38360_G24.Text = arrtmp[147].ToString(); NT38360_B24.Text = arrtmp[149].ToString();

            }
        }
        private void NT38360_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[40];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 40;

            Send_buf[0] = 0x34;        //NT38360
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "NT38360_RegR":
                    {
                        string strLine = Convert.ToString(NT38360_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(tb_NT38360_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x34;
                        Send_buf[1] = 0x02; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "NT38360_RegW":
                    {
                        string strLine = Convert.ToString(NT38360_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x34; //
                        Send_buf[1] = 0x01; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "btn_NT38360_gamma_point_select":
                    {
                        Send_buf[0] = 0x34;
                        Send_buf[1] = 0x06; //重置绑点位置
                        Send_buf[2] = 0x23; //33+2cmd = 35;
                        /*
                        Send_buf[3] = Convert.ToByte(NT38360_Ponit_Num.Value); //绑点个数
                        Send_buf[4] = Convert.ToByte(NT38360_0.Value);
                        Send_buf[5] = Convert.ToByte(NT38360_1.Value);
                        Send_buf[6] = Convert.ToByte(NT38360_2.Value);
                        Send_buf[7] = Convert.ToByte(NT38360_3.Value);
                        Send_buf[8] = Convert.ToByte(NT38360_4.Value);
                        Send_buf[9] = Convert.ToByte(NT38360_5.Value);
                        Send_buf[10] = Convert.ToByte(NT38360_6.Value);
                        Send_buf[11] = Convert.ToByte(NT38360_7.Value);
                        Send_buf[12] = Convert.ToByte(NT38360_8.Value);
                        Send_buf[13] = Convert.ToByte(NT38360_9.Value);
                        Send_buf[14] = Convert.ToByte(NT38360_10.Value);
                        Send_buf[15] = Convert.ToByte(NT38360_11.Value);
                        Send_buf[16] = Convert.ToByte(NT38360_12.Value);
                        Send_buf[17] = Convert.ToByte(NT38360_13.Value);
                        Send_buf[18] = Convert.ToByte(NT38360_14.Value);
                        Send_buf[19] = Convert.ToByte(NT38360_15.Value);
                        Send_buf[20] = Convert.ToByte(NT38360_16.Value);
                        Send_buf[21] = Convert.ToByte(NT38360_17.Value);
                        Send_buf[22] = Convert.ToByte(NT38360_18.Value);
                        Send_buf[23] = Convert.ToByte(NT38360_19.Value);
                        Send_buf[24] = Convert.ToByte(NT38360_20.Value);
                        Send_buf[25] = Convert.ToByte(NT38360_21.Value);
                        Send_buf[26] = Convert.ToByte(NT38360_22.Value);
                        Send_buf[27] = Convert.ToByte(NT38360_23.Value);
                        Send_buf[28] = Convert.ToByte(NT38360_24.Value);
                        Send_buf[29] = Convert.ToByte(NT38360_25.Value);
                        Send_buf[30] = Convert.ToByte(NT38360_26.Value);
                        Send_buf[31] = Convert.ToByte(NT38360_27.Value);
                        Send_buf[32] = Convert.ToByte(NT38360_28.Value);
                        Send_buf[33] = Convert.ToByte(NT38360_29.Value);
                        Send_buf[34] = Convert.ToByte(NT38360_30.Value);
                        Send_buf[35] = Convert.ToByte(NT38360_31.Value);
                        Send_buf[36] = Convert.ToByte(NT38360_32.Value);
                        */
                    }
                    break;
                case "NT38360_Reg_OTP":       //          B
                    {

                    }
                    break;
                case "OTP_Time_W":         //w写寄存器   C
                    {
                        if (Convert.ToDecimal(NT38360_Time.Text) > 4)
                        {
                            MessageBox.Show("OTP次数不能大于4次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(NT38360_Time.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 800;
            frmHelp.T1.Enabled = true;
        }
        private void NT38360_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[58];
            Setting.TempRecord.cmdLen = 58;
            Send_buf[2] = 53; //56-5
            Send_buf[3] = 0;
            Send_buf[0] = 0x34; //NT38360

            switch (btnTmp.Name)
            {
                case "NT38360_GammaR_R":
                    {
                        Setting.TempRecord.FT2306CmdType = 0x03;       //      3
                        Send_buf[1] = 0x04;                             //读 Red Gamma
                        Send_buf[3] = 0x00;

                    }
                    break;
                case "NT38360_GammaR_G":
                    {
                        Setting.TempRecord.FT2306CmdType = 0x04;   //      4
                        Send_buf[1] = 0x04;                         //  读Gamma
                        Send_buf[3] = 0x01;                        //绿色 green

                    }
                    break;
                case "NT38360_GammaR_B": //              9
                    {
                        Setting.TempRecord.FT2306CmdType = 0x05;   //      5
                        Send_buf[1] = 0x04;                         //   读Gamma
                        Send_buf[3] = 0x02;                        //   blue
                    }
                    break;
                //--------------------------------------
                case "NT38360_GammaW_R":
                    {
                        Send_buf[1] = 0x03;    //写Gamma
                        Send_buf[3] = 0x00;    //红色
                        Send_buf[4] = Convert.ToByte(Gamma_Band_Select.Text);
                        Send_buf[5] = Gamma_node_index;

                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT38360_R0.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NT38360_R0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT38360_R1.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NT38360_R1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT38360_R2.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NT38360_R2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT38360_R3.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NT38360_R3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT38360_R4.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NT38360_R4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT38360_R5.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NT38360_R5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT38360_R6.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NT38360_R6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT38360_R7.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NT38360_R7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT38360_R8.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NT38360_R8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT38360_R9.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NT38360_R9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT38360_R10.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NT38360_R10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT38360_R11.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NT38360_R11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT38360_R12.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NT38360_R12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT38360_R13.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NT38360_R13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT38360_R14.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NT38360_R14.Value.ToString())) & 0xff);
                        // if (start_15gamma == false)
                        // {
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT38360_R15.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NT38360_R15.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT38360_R16.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NT38360_R16.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT38360_R17.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NT38360_R17.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT38360_R18.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NT38360_R18.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT38360_R19.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NT38360_R19.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT38360_R20.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NT38360_R20.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT38360_R21.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NT38360_R21.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT38360_R22.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NT38360_R22.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT38360_R23.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NT38360_R23.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT38360_R24.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NT38360_R24.Value.ToString())) & 0xff);
                        // }
                    }
                    break;
                case "NT38360_GammaW_G":
                    {
                        Send_buf[1] = 0x03;    //写Gamma
                        Send_buf[3] = 0x01;    //绿色
                        Send_buf[4] = Convert.ToByte(Gamma_Band_Select.Text);
                        Send_buf[5] = Gamma_node_index;

                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT38360_G0.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NT38360_G0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT38360_G1.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NT38360_G1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT38360_G2.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NT38360_G2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT38360_G3.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NT38360_G3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT38360_G4.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NT38360_G4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT38360_G5.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NT38360_G5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT38360_G6.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NT38360_G6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT38360_G7.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NT38360_G7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT38360_G8.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NT38360_G8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT38360_G9.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NT38360_G9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT38360_G10.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NT38360_G10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT38360_G11.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NT38360_G11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT38360_G12.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NT38360_G12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT38360_G13.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NT38360_G13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT38360_G14.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NT38360_G14.Value.ToString())) & 0xff);
                        // if (start_15gamma == false)
                        // {
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT38360_G15.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NT38360_G15.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT38360_G16.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NT38360_G16.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT38360_G17.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NT38360_G17.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT38360_G18.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NT38360_G18.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT38360_G19.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NT38360_G19.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT38360_G20.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NT38360_G20.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT38360_G21.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NT38360_G21.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT38360_G22.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NT38360_G22.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT38360_G23.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NT38360_G23.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT38360_G24.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NT38360_G24.Value.ToString())) & 0xff);
                        // }

                    }
                    break;
                case "NT38360_GammaW_B":
                    {
                        Send_buf[1] = 0x03;    //写Gamma
                        Send_buf[3] = 0x02;    //蓝色
                        Send_buf[4] = Convert.ToByte(Gamma_Band_Select.Text);
                        Send_buf[5] = Gamma_node_index;

                        Send_buf[6] = Convert.ToByte((Convert.ToUInt16(NT38360_B0.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NT38360_B0.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte((Convert.ToUInt16(NT38360_B1.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NT38360_B1.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte((Convert.ToUInt16(NT38360_B2.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NT38360_B2.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte((Convert.ToUInt16(NT38360_B3.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NT38360_B3.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte((Convert.ToUInt16(NT38360_B4.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NT38360_B4.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte((Convert.ToUInt16(NT38360_B5.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NT38360_B5.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte((Convert.ToUInt16(NT38360_B6.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NT38360_B6.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte((Convert.ToUInt16(NT38360_B7.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NT38360_B7.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte((Convert.ToUInt16(NT38360_B8.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NT38360_B8.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte((Convert.ToUInt16(NT38360_B9.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NT38360_B9.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte((Convert.ToUInt16(NT38360_B10.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NT38360_B10.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte((Convert.ToUInt16(NT38360_B11.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NT38360_B11.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte((Convert.ToUInt16(NT38360_B12.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NT38360_B12.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte((Convert.ToUInt16(NT38360_B13.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NT38360_B13.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte((Convert.ToUInt16(NT38360_B14.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NT38360_B14.Value.ToString())) & 0xff);
                        // if (start_15gamma == false)
                        // {
                        Send_buf[36] = Convert.ToByte((Convert.ToUInt16(NT38360_B15.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NT38360_B15.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte((Convert.ToUInt16(NT38360_B16.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NT38360_B16.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte((Convert.ToUInt16(NT38360_B17.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NT38360_B17.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte((Convert.ToUInt16(NT38360_B18.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NT38360_B18.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte((Convert.ToUInt16(NT38360_B19.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NT38360_B19.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte((Convert.ToUInt16(NT38360_B20.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NT38360_B20.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte((Convert.ToUInt16(NT38360_B21.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NT38360_B21.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte((Convert.ToUInt16(NT38360_B22.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NT38360_B22.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte((Convert.ToUInt16(NT38360_B23.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NT38360_B23.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte((Convert.ToUInt16(NT38360_B24.Value.ToString()) >> 8) & 0x0f);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NT38360_B24.Value.ToString())) & 0xff);
                        // }

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[56], out Send_buf[57]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 58);   //NT38360
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void btn_NT38360_Calculate_Click(object sender, EventArgs e)
        {
            // double Lr_d, Lg_d, Lb_d;
            double L_X, L_Y, L_Z;
            string[] arr = NT38360_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txtNT38360_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtNT38360_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtNT38360_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txtNT38360_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtNT38360_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtNT38360_X.Text = Diven_Gamma[0].X.ToString();
                txtNT38360_Y.Text = Diven_Gamma[0].Y.ToString();
                Points_LV.Text = "";

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点       

                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    Points_LV.Text = Points_LV.Text + Diven_Offset[i].NO + ":  " + Math.Round(D_lum[i], 6) + "\r\n";
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }

            //btnCal_NT38370_Position_Click(sender, e);
        }
        //自动调节启动开关
        private static byte Gamma_Auto_flag_dll = 0;
        private void Load_GammmPic(string filenames)      //双击 载入 BMP图片
        {
            Int32 HACT_True = 0;
            Int32 HACT_Skip = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;

            //byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;

            string FileName = Gamma_loading_path.Text + '\\' + filenames + ".bmp";
            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

            HACT_Skip = HACT_True;
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                frmHelp.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            //-----------------------------------
            try
            {
                j = 1;
                aFile.Seek(54, SeekOrigin.Begin);
                while (j <= VACT_True)
                {
                    if (HACT_True % 4 != 0)
                        aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                    else
                        aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                    ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                    try
                    {
                        frmHelp.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        break;
                    }
                    m_txlen += ByteReaded;
                    //     frmHelp.progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                    //     frmHelp.label14.Text = frmHelp.progressBar1.Value.ToString() + "%";
                    //    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    j++;
                }

                TemFileContent[0] = 0x5A;
                try
                {
                    frmHelp.comPort.Write(TemFileContent, 0, 1);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                aFile.Close();
            }
            catch
            {
                frmHelp.textBox1.Text += "啥图片都没显示";
                frmHelp.textBox1.Select(frmHelp.textBox1.Text.Length, 0);
                frmHelp.textBox1.ScrollToCaret();
            }
        }

        private void NT38360_Bist_Pattern(decimal R, decimal G, decimal B)
        {
            NT38360_Reg_Data.Text = "ff,25";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "E8,00,00,00,00,00,00,00,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "E9,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "E9,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "EA," + ((byte)(R)).ToString("x") + "," + ((byte)(G)).ToString("x") + "," + ((byte)(B)).ToString("x");
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "EB,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
            NT38360_Reg_Data.Text = "EC,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(15);
        }

        private void NT38360_AUTO_gamma(Object myObject, EventArgs myEventArgs)
        {
            double L_X, L_Y, L_Z, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((frmHelp.CASDK_LUM < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (frmHelp.CASDK_LUM > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((frmHelp.CASDK_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (frmHelp.CASDK_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((frmHelp.CASDK_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (frmHelp.CASDK_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index == Diven_Gamma[0].Points_num)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "NT38360 gamma调试完毕!\r\n";
                    //---------------------------------add-------------------------------------
                    if (Multi_gamma_txt_next_index < Multi_gamma_txt_line_count)
                    {
                        Multi_gamma_txt_now_index = Multi_gamma_txt_next_index;
                        Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
                    }
                    //---------------------------------add-------------------------------------
                }

                if (index < Diven_Gamma[0].Points_num) //绑点切换部分
                {
                    if (cBx_AOD_Auto_Gamma.Checked == true)  //AOD gamma adjust in AOD mode or not
                    {
                        Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
                    }
                    else
                    {
                        //frmHelp.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        //frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                        //frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                        //frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                        NT38360_Bist_Pattern(Diven_Offset[index].NO, Diven_Offset[index].NO, Diven_Offset[index].NO);

                    }
                    Thread.Sleep(15);
                    frmHelp.Msr_CASDK();
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                    T20.Enabled = true;
                }

            }
            else  //调整部分
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.Lvxy_to_XYZ(frmHelp.CASDK_LUM, frmHelp.CASDK_x, frmHelp.CASDK_y, out L_X, out L_Y, out L_Z);
                lumr = L_X;
                lumg = L_Y;
                lumb = L_Z;
                try
                {
                    switch (index)
                    {
                        case 0: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = 180;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R24.Value, NT38360_G24.Value, NT38360_B24.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R24.Value = r;
                                NT38360_G24.Value = g;
                                NT38360_B24.Value = b;
                                if (NT38360_R24.Value <= NT38360_R23.Value)
                                {
                                    NT38360_R23.Value = r - 5;
                                }
                                if (NT38360_G24.Value <= NT38360_G23.Value)
                                {
                                    NT38360_G23.Value = g - 5;
                                }
                                if (NT38360_B24.Value <= NT38360_B23.Value)
                                {
                                    NT38360_B23.Value = b - 5;
                                }
                            }
                            break;
                        case 1: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 10 ? 35 : 70;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R23.Value, NT38360_G23.Value, NT38360_B23.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R23.Value = r;
                                NT38360_G23.Value = g;
                                NT38360_B23.Value = b;
                                if (NT38360_R23.Value <= NT38360_R22.Value)
                                {
                                    NT38360_R22.Value = r - 5;
                                }
                                if (NT38360_G23.Value <= NT38360_G22.Value)
                                {
                                    NT38360_G22.Value = g - 5;
                                }
                                if (NT38360_B23.Value <= NT38360_B22.Value)
                                {
                                    NT38360_B22.Value = b - 5;
                                }
                            }
                            break;
                        case 2: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R22.Value, NT38360_G22.Value, NT38360_B22.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R22.Value = r;
                                NT38360_G22.Value = g;
                                NT38360_B22.Value = b;
                                if (NT38360_R22.Value <= NT38360_R21.Value)
                                {
                                    NT38360_R21.Value = r - 5;
                                }
                                if (NT38360_G22.Value <= NT38360_G21.Value)
                                {
                                    NT38360_G21.Value = g - 5;
                                }
                                if (NT38360_B22.Value <= NT38360_B21.Value)
                                {
                                    NT38360_B21.Value = b - 5;
                                }
                            }
                            break;
                        case 3: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R21.Value, NT38360_G21.Value, NT38360_B21.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R21.Value = r;
                                NT38360_G21.Value = g;
                                NT38360_B21.Value = b;
                                if (NT38360_R21.Value <= NT38360_R20.Value)
                                {
                                    NT38360_R20.Value = r - 5;
                                }
                                if (NT38360_G21.Value <= NT38360_G20.Value)
                                {
                                    NT38360_G20.Value = g - 5;
                                }
                                if (NT38360_B21.Value <= NT38360_B20.Value)
                                {
                                    NT38360_B20.Value = b - 5;
                                }
                            }
                            break;
                        case 4: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R20.Value, NT38360_G20.Value, NT38360_B20.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R20.Value = r;
                                NT38360_G20.Value = g;
                                NT38360_B20.Value = b;
                                if (NT38360_R20.Value <= NT38360_R19.Value)
                                {
                                    NT38360_R19.Value = r - 5;
                                }
                                if (NT38360_G20.Value <= NT38360_G19.Value)
                                {
                                    NT38360_G19.Value = g - 5;
                                }
                                if (NT38360_B20.Value <= NT38360_B19.Value)
                                {
                                    NT38360_B19.Value = b - 5;
                                }
                            }
                            break;
                        case 5: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R19.Value, NT38360_G19.Value, NT38360_B19.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R19.Value = r;
                                NT38360_G19.Value = g;
                                NT38360_B19.Value = b;
                                if (NT38360_R19.Value <= NT38360_R18.Value)
                                {
                                    NT38360_R18.Value = r - 5;
                                }
                                if (NT38360_G19.Value <= NT38360_G18.Value)
                                {
                                    NT38360_G18.Value = g - 5;
                                }
                                if (NT38360_B19.Value <= NT38360_B18.Value)
                                {
                                    NT38360_B18.Value = b - 5;
                                }
                            }
                            break;
                        case 6: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R18.Value, NT38360_G18.Value, NT38360_B18.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R18.Value = r;
                                NT38360_G18.Value = g;
                                NT38360_B18.Value = b;
                                if (NT38360_R18.Value <= NT38360_R17.Value)
                                {
                                    NT38360_R17.Value = r - 5;
                                }
                                if (NT38360_G18.Value <= NT38360_G17.Value)
                                {
                                    NT38360_G17.Value = g - 5;
                                }
                                if (NT38360_B18.Value <= NT38360_B17.Value)
                                {
                                    NT38360_B17.Value = b - 5;
                                }
                            }
                            break;
                        case 7: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R17.Value, NT38360_G17.Value, NT38360_B17.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R17.Value = r;
                                NT38360_G17.Value = g;
                                NT38360_B17.Value = b;
                                if (NT38360_R17.Value <= NT38360_R16.Value)
                                {
                                    NT38360_R16.Value = r - 5;
                                }
                                if (NT38360_G17.Value <= NT38360_G16.Value)
                                {
                                    NT38360_G16.Value = g - 5;
                                }
                                if (NT38360_B17.Value <= NT38360_B16.Value)
                                {
                                    NT38360_B16.Value = b - 5;
                                }
                            }
                            break;
                        case 8: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R16.Value, NT38360_G16.Value, NT38360_B16.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R16.Value = r;
                                NT38360_G16.Value = g;
                                NT38360_B16.Value = b;
                                if (NT38360_R16.Value <= NT38360_R15.Value)
                                {
                                    NT38360_R15.Value = r - 5;
                                }
                                if (NT38360_G16.Value <= NT38360_G15.Value)
                                {
                                    NT38360_G15.Value = g - 5;
                                }
                                if (NT38360_B16.Value <= NT38360_B15.Value)
                                {
                                    NT38360_B15.Value = b - 5;
                                }
                            }
                            break;
                        case 9: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R15.Value, NT38360_G15.Value, NT38360_B15.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R15.Value = r;
                                NT38360_G15.Value = g;
                                NT38360_B15.Value = b;
                                if (NT38360_R15.Value <= NT38360_R14.Value)
                                {
                                    NT38360_R14.Value = r - 5;
                                }
                                if (NT38360_G15.Value <= NT38360_G14.Value)
                                {
                                    NT38360_G14.Value = g - 5;
                                }
                                if (NT38360_B15.Value <= NT38360_B14.Value)
                                {
                                    NT38360_B14.Value = b - 5;
                                }
                            }
                            break;

                        case 10: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R14.Value, NT38360_G14.Value, NT38360_B14.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R14.Value = r;
                                NT38360_G14.Value = g;
                                NT38360_B14.Value = b;
                                if (NT38360_R14.Value <= NT38360_R13.Value)
                                {
                                    NT38360_R13.Value = r - 5;
                                }
                                if (NT38360_G14.Value <= NT38360_G13.Value)
                                {
                                    NT38360_G13.Value = g - 5;
                                }
                                if (NT38360_B14.Value <= NT38360_B13.Value)
                                {
                                    NT38360_B13.Value = b - 5;
                                }
                            }
                            break;

                        case 11: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R13.Value, NT38360_G13.Value, NT38360_B13.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R13.Value = r;
                                NT38360_G13.Value = g;
                                NT38360_B13.Value = b;
                                if (NT38360_R13.Value <= NT38360_R12.Value)
                                {
                                    NT38360_R12.Value = r - 5;
                                }
                                if (NT38360_G13.Value <= NT38360_G12.Value)
                                {
                                    NT38360_G12.Value = g - 5;
                                }
                                if (NT38360_B13.Value <= NT38360_B12.Value)
                                {
                                    NT38360_B12.Value = b - 5;
                                }
                            }
                            break;

                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R12.Value, NT38360_G12.Value, NT38360_B12.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R12.Value = r;
                                NT38360_G12.Value = g;
                                NT38360_B12.Value = b;
                                if (NT38360_R12.Value <= NT38360_R11.Value)
                                {
                                    NT38360_R11.Value = r - 5;
                                }
                                if (NT38360_G12.Value <= NT38360_G11.Value)
                                {
                                    NT38360_G11.Value = g - 5;
                                }
                                if (NT38360_B12.Value <= NT38360_B11.Value)
                                {
                                    NT38360_B11.Value = b - 5;
                                }
                            }
                            break;
                        case 13: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 10 ? 25 : 70;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R11.Value, NT38360_G11.Value, NT38360_B11.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R11.Value = r;
                                NT38360_G11.Value = g;
                                NT38360_B11.Value = b;
                                if (NT38360_R11.Value <= NT38360_R10.Value)
                                {
                                    NT38360_R10.Value = r - 5;
                                }
                                if (NT38360_G11.Value <= NT38360_G10.Value)
                                {
                                    NT38360_G10.Value = g - 5;
                                }
                                if (NT38360_B11.Value <= NT38360_B10.Value)
                                {
                                    NT38360_B10.Value = b - 5;
                                }
                            }
                            break;
                        case 14: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R10.Value, NT38360_G10.Value, NT38360_B10.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R10.Value = r;
                                NT38360_G10.Value = g;
                                NT38360_B10.Value = b;
                                if (NT38360_R10.Value <= NT38360_R9.Value)
                                {
                                    NT38360_R9.Value = r - 5;
                                }
                                if (NT38360_G10.Value <= NT38360_G9.Value)
                                {
                                    NT38360_G9.Value = g - 5;
                                }
                                if (NT38360_B10.Value <= NT38360_B9.Value)
                                {
                                    NT38360_B9.Value = b - 5;
                                }
                            }
                            break;
                        case 15: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R9.Value, NT38360_G9.Value, NT38360_B9.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R9.Value = r;
                                NT38360_G9.Value = g;
                                NT38360_B9.Value = b;
                                if (NT38360_R9.Value <= NT38360_R8.Value)
                                {
                                    NT38360_R8.Value = r - 5;
                                }
                                if (NT38360_G9.Value <= NT38360_G8.Value)
                                {
                                    NT38360_G8.Value = g - 5;
                                }
                                if (NT38360_B9.Value <= NT38360_B8.Value)
                                {
                                    NT38360_B8.Value = b - 5;
                                }
                            }
                            break;
                        case 16: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R8.Value, NT38360_G8.Value, NT38360_B8.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R8.Value = r;
                                NT38360_G8.Value = g;
                                NT38360_B8.Value = b;
                                if (NT38360_R8.Value <= NT38360_R7.Value)
                                {
                                    NT38360_R7.Value = r - 5;
                                }
                                if (NT38360_G8.Value <= NT38360_G7.Value)
                                {
                                    NT38360_G7.Value = g - 5;
                                }
                                if (NT38360_B8.Value <= NT38360_B7.Value)
                                {
                                    NT38360_B7.Value = b - 5;
                                }
                            }
                            break;
                        case 17: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 10 ? 25 : 65;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R7.Value, NT38360_G7.Value, NT38360_B7.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R7.Value = r;
                                NT38360_G7.Value = g;
                                NT38360_B7.Value = b;
                                if (NT38360_R7.Value <= NT38360_R6.Value)
                                {
                                    NT38360_R6.Value = r - 5;
                                }
                                if (NT38360_G7.Value <= NT38360_G6.Value)
                                {
                                    NT38360_G6.Value = g - 5;
                                }
                                if (NT38360_B7.Value <= NT38360_B6.Value)
                                {
                                    NT38360_B6.Value = b - 5;
                                }
                            }
                            break;
                        case 18: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R6.Value, NT38360_G6.Value, NT38360_B6.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R6.Value = r;
                                NT38360_G6.Value = g;
                                NT38360_B6.Value = b;
                                if (NT38360_R6.Value <= NT38360_R5.Value)
                                {
                                    NT38360_R5.Value = r - 5;
                                }
                                if (NT38360_G6.Value <= NT38360_G5.Value)
                                {
                                    NT38360_G5.Value = g - 5;
                                }
                                if (NT38360_B6.Value <= NT38360_B5.Value)
                                {
                                    NT38360_B5.Value = b - 5;
                                }
                            }
                            break;
                        case 19: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R5.Value, NT38360_G5.Value, NT38360_B5.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R5.Value = r;
                                NT38360_G5.Value = g;
                                NT38360_B5.Value = b;
                                if (NT38360_R5.Value <= NT38360_R4.Value)
                                {
                                    NT38360_R4.Value = r - 5;
                                }
                                if (NT38360_G5.Value <= NT38360_G4.Value)
                                {
                                    NT38360_G4.Value = g - 5;
                                }
                                if (NT38360_B5.Value <= NT38360_B4.Value)
                                {
                                    NT38360_B4.Value = b - 5;
                                }
                                //  NT38360_auto_gamma_write(gamma_r[index], Convert.ToUInt16(NT38360_R5.Value), gamma_g[index], Convert.ToUInt16(NT38360_G5.Value), gamma_b[index], Convert.ToUInt16(NT38360_B5.Value));
                            }
                            break;
                        case 20: //9
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R4.Value, NT38360_G4.Value, NT38360_B4.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R4.Value = r;
                                NT38360_G4.Value = g;
                                NT38360_B4.Value = b;
                                if (NT38360_R4.Value <= NT38360_R3.Value)
                                {
                                    NT38360_R3.Value = r - 5;
                                }
                                if (NT38360_G4.Value <= NT38360_G3.Value)
                                {
                                    NT38360_G3.Value = g - 5;
                                }
                                if (NT38360_B4.Value <= NT38360_B3.Value)
                                {
                                    NT38360_B3.Value = b - 5;
                                }
                                //  NT38360_auto_gamma_write(gamma_r[index], Convert.ToUInt16(NT38360_R4.Value), gamma_g[index], Convert.ToUInt16(NT38360_G4.Value), gamma_b[index], Convert.ToUInt16(NT38360_B4.Value));
                            }
                            break;
                        case 21: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 10 ? 20 : 55;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R3.Value, NT38360_G3.Value, NT38360_B3.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R3.Value = r;
                                NT38360_G3.Value = g;
                                NT38360_B3.Value = b;
                                // NT38360_auto_gamma_write(gamma_r[index], Convert.ToUInt16(NT38360_R3.Value), gamma_g[index], Convert.ToUInt16(NT38360_G3.Value), gamma_b[index], Convert.ToUInt16(NT38360_B3.Value));

                                if (NT38360_R3.Value <= NT38360_R2.Value)
                                {
                                    NT38360_R2.Value = r - 1;
                                }
                                if (NT38360_G3.Value <= NT38360_G2.Value)
                                {
                                    NT38360_G2.Value = g - 1;
                                }
                                if (NT38360_B3.Value <= NT38360_B2.Value)
                                {
                                    NT38360_B2.Value = b - 1;
                                }
                            }
                            break;
                        case 22: //9
                            {
                                frmHelp.Gamma_auto_adjust_count_set = lumg < 10 ? 15 : 40;
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R2.Value, NT38360_G2.Value, NT38360_B2.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R2.Value = r;
                                NT38360_G2.Value = g;
                                NT38360_B2.Value = b;
                                if (NT38360_R2.Value <= NT38360_R1.Value)
                                {
                                    NT38360_R1.Value = r - 1;
                                }
                                if (NT38360_G2.Value <= NT38360_G1.Value)
                                {
                                    NT38360_G1.Value = g - 1;
                                }
                                if (NT38360_B2.Value <= NT38360_B1.Value)
                                {
                                    NT38360_B1.Value = b - 1;
                                }
                                //  NT38360_auto_gamma_write(gamma_r[index], Convert.ToUInt16(NT38360_R2.Value), gamma_g[index], Convert.ToUInt16(NT38360_G2.Value), gamma_b[index], Convert.ToUInt16(NT38360_B2.Value));
                            }
                            break;
                        case 23: //9
                            {
                                Gamma_adjust_timeout_flag = true;
                                //  frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], NT38360_R1.Value, NT38360_G1.Value, NT38360_B1.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NT38360_R1.Value = (int)NT38360_R2.Value / 2;
                                NT38360_G1.Value = (int)NT38360_G2.Value / 2;
                                NT38360_B1.Value = (int)NT38360_B2.Value / 2;

                                r = NT38360_R1.Value;
                                g = NT38360_G1.Value;
                                b = NT38360_B1.Value;
                                //  NT38360_auto_gamma_write(gamma_r[index], Convert.ToUInt16(NT38360_R1.Value), gamma_g[index], Convert.ToUInt16(NT38360_G1.Value), gamma_b[index], Convert.ToUInt16(NT38360_B1.Value));
                            }
                            break;
                        case 24: //
                            {
                                Gamma_adjust_timeout_flag = true;
                                NT38360_R0.Value = 0;
                                NT38360_G0.Value = 0;
                                NT38360_B0.Value = 0;
                                r = NT38360_R1.Value;
                                g = NT38360_G1.Value;
                                b = NT38360_B1.Value;
                            }
                            break;
                        default: break;
                    }
                    Gamma_node_index = (byte)(24 - index);
                    //NT38360_GammaW_Code_Via(Gamma_node_index, r, g, b);
                    //Thread.Sleep(60);
                    NT38360_Gamma_Click(NT38360_GammaW_R, null);
                    Thread.Sleep(30);
                    NT38360_Gamma_Click(NT38360_GammaW_G, null);
                    Thread.Sleep(30);
                    NT38360_Gamma_Click(NT38360_GammaW_B, null);
                    Thread.Sleep(30);
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }



        private void NT38360_GammaW_Code_Via(byte node_index, decimal r, decimal g, decimal b)
        {
            Send_buf = new byte[14];
            Setting.TempRecord.cmdLen = 14;
            Send_buf[0] = 0x34; //NT38360
            Send_buf[1] = 0x05;    //写Gamma
            Send_buf[2] = 9; //56-5
            Send_buf[3] = 0;

            Send_buf[4] = Convert.ToByte(Gamma_Band_Select.Text);
            Send_buf[5] = node_index;

            Send_buf[6] = Convert.ToByte(((UInt16)(r) >> 8) & 0x0f);
            Send_buf[7] = Convert.ToByte((UInt16)(r) & 0xff);
            Send_buf[8] = Convert.ToByte(((UInt16)(g) >> 8) & 0x0f);
            Send_buf[9] = Convert.ToByte((UInt16)(g) & 0xff);
            Send_buf[10] = Convert.ToByte(((UInt16)(b) >> 8) & 0x0f);
            Send_buf[11] = Convert.ToByte((UInt16)(b) & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[12], out Send_buf[13]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 14);   //NT38360
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 1000;
            frmHelp.T1.Enabled = true;
        }

        private void NT38360_Gamma_Load(object sender, EventArgs e)
        {

        }

        private void T20_Tick(object sender, EventArgs e)
        {
            NT38360_AUTO_gamma(null, null);
        }

        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        if (frmHelp.cbxlogic_go.Checked == true)
                        {
                            Send_buf[1] = 0x18;
                            Send_buf[3] = (byte)(frmHelp.CBX_HFZ_LOGIC.SelectedIndex);
                            if (frmHelp.cbxlogic_go_rgb_swap.Checked == true)//udc
                            {
                                Send_buf[4] = (byte)(frmHelp.numPatternR.Value);
                                Send_buf[5] = (byte)(frmHelp.numPatternG.Value);
                                Send_buf[6] = (byte)(frmHelp.numPatternB.Value);
                                Send_buf[7] = (byte)(frmHelp.HFZ_M_R.Value);
                                Send_buf[8] = (byte)(frmHelp.HFZ_M_G.Value);
                                Send_buf[9] = (byte)(frmHelp.HFZ_M_B.Value);
                            }
                            else
                            {
                                Send_buf[4] = (byte)(frmHelp.HFZ_M_R.Value);
                                Send_buf[5] = (byte)(frmHelp.HFZ_M_G.Value);
                                Send_buf[6] = (byte)(frmHelp.HFZ_M_B.Value);
                                Send_buf[7] = (byte)(frmHelp.numPatternR.Value);
                                Send_buf[8] = (byte)(frmHelp.numPatternG.Value);
                                Send_buf[9] = (byte)(frmHelp.numPatternB.Value);
                            }
                            Send_buf[10] = (byte)((int)(frmHelp.HFZ_X0.Value) >> 8);
                            Send_buf[11] = (byte)((int)(frmHelp.HFZ_X0.Value));
                            Send_buf[12] = (byte)((int)(frmHelp.HFZ_X1.Value) >> 8);
                            Send_buf[13] = (byte)((int)(frmHelp.HFZ_X1.Value));
                            Send_buf[14] = (byte)((int)(frmHelp.HFZ_Y0.Value) >> 8);
                            Send_buf[15] = (byte)((int)(frmHelp.HFZ_Y0.Value));
                            Send_buf[16] = (byte)((int)(frmHelp.HFZ_Y1.Value) >> 8);
                            Send_buf[17] = (byte)((int)(frmHelp.HFZ_Y1.Value));
                            Send_buf[18] = (byte)((int)(frmHelp.HFZ_R.Value) >> 8);
                            Send_buf[19] = (byte)((int)(frmHelp.HFZ_R.Value));
                        }
                        else
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[3] = byte.Parse(frmHelp.numPatternR.Value.ToString());
                            Send_buf[4] = byte.Parse(frmHelp.numPatternG.Value.ToString());
                            Send_buf[5] = byte.Parse(frmHelp.numPatternB.Value.ToString());
                        }
                    }
                    break;
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 25);
                return;
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 1000;
            frmHelp.T1.Enabled = true;
        }


        private void Btn_load_gamma_patten_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            Gamma_loading_path.Text = fldb.SelectedPath;

            if (!Directory.Exists(Gamma_loading_path.Text))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
        }

        private void NT38360_AUTO_gamma_stop(object sender, EventArgs e)
        {
            T20.Enabled = false;
        }

        private int Multi_gamma_txt_now_index = 0;
        private int Multi_gamma_txt_next_index;
        private int Multi_gamma_txt_line_count = 0;
        private int Multi_gamma_group_index = 0;
        private void Btn_flexible_gamma_Click(object sender, EventArgs e)
        {
            btn_NT38360_Calculate_Click(null, null);
            Multi_gamma_txt_now_index = 0;
            Multi_gamma_txt_line_count = this.NT38360_Gamma_SetBox.Lines.GetUpperBound(0);
            Multi_flexible_gammas(Multi_gamma_txt_now_index, out Multi_gamma_txt_next_index);
        }
        private void btnAutoGamma_NT38360_Click(object sender, EventArgs e)//255阶为例
        {
            double L_X, L_Y, L_Z;
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------

            if (cBx_AOD_Auto_Gamma.Checked == true)  //AOD gamma adjust in AOD mode or not
            {
                Load_GammmPic(Diven_Offset[index].NO.ToString()); //Load pic
            }
            else
            {
                //frmHelp.numPatternR.Value = Diven_Offset[index].NO;
                //frmHelp.numPatternG.Value = Diven_Offset[index].NO;
                //frmHelp.numPatternB.Value = Diven_Offset[index].NO;
                //frmHelp.OLED_Show_Click(frmHelp.btnPatternW, e);
                NT38360_Bist_Pattern(Diven_Offset[index].NO, Diven_Offset[index].NO, Diven_Offset[index].NO);
            }
            Thread.Sleep(15);
            T20.Enabled = true;
        }
        private void Multi_flexible_gammas(int txt_index, out int list_view_index)
        {
            string strLine = Convert.ToString(NT38360_Gamma_SetBox.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            string perline = string.Empty;
            string perline0 = string.Empty;
            list_view_index = 0;

            if (NT38360_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else  //标记关键事件
            {
                try
                {
                    arr = strLine.Split('\n'); //以逗号分割，添加到数
                    for (int i = txt_index; i < arr.Length;)  //获取的行数
                    {
                        perline = arr[i];
                        perline = perline.Replace(" ", "");
                        perline = perline.Replace(";", "");
                        perline = perline.Replace("(", "");
                        perline = perline.Replace(")", "");
                        perline = perline.Replace("\r", "");
                        perline = perline.ToUpper();

                        if (perline.Contains("REGW"))
                        {
                            perline = perline.Replace("REGW", "");
                            NT38360_Reg_Data.Text = perline;
                            NT38360_Others_Click(NT38360_RegW, null);
                            Thread.Sleep(20);
                            i = i + 1;
                        }
                        else if (perline.Contains("AOD_IN"))
                        {
                            //   cBx_AOD_Auto_Gamma.Checked = true;
                            //  i = i + 1;
                        }
                        else if (perline.Contains("AOD_OUT"))
                        {
                            //  cBx_AOD_Auto_Gamma.Checked = false;
                            // i = i + 1;
                        }
                        else if (perline.Contains("SAVE"))
                        {
                            string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                            if (Directory.Exists(pathsave_file) == false)
                            {
                                Directory.CreateDirectory(pathsave_file);
                            }
                            string pathsave = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                            Multi_SaveGamma(pathsave);
                            Thread.Sleep(50);
                            i = i + 1;
                        }
                        else if (perline.Contains("GAMMA_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = false;
                            perline = perline.Replace("GAMMA_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_NT38360_Normal.Checked = true;
                            //    NT38360_Exit_Gamma_Click(null, null);
                            NT38360_Inter_Gamma_Click(null, null);
                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();

                            }
                            catch {; }
                            if (Total_points > 15)
                            {
                                //  rbt_VT6110_P25.Checked = true;
                            }
                            else
                            {
                                // rbt_VT6110_IRC.Checked = true;
                            }
                            btnAutoGamma_NT38360_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }

                        else if (perline.Contains("CUP_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = false;
                            perline = perline.Replace("CUP_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_NT38360_Normal.Checked = true;
                            //NT38360_Exit_Gamma_Click(null, null);
                            //NT38360_Inter_Gamma_Click(null, null);
                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();

                            }
                            catch {; }

                            btnAutoGamma_NT38360_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }
                        else if (perline.Contains("AOD_SET"))
                        {
                            frmHelp.cbxlogic_go.Checked = true;
                            perline = perline.Replace("AOD_SET", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_NT38360_Normal.Checked = true;
                            //    NT38360_Exit_Gamma_Click(null, null);
                            NT38360_Inter_Gamma_Click(null, null);
                            ArrayList list = new ArrayList();
                            do
                            {
                                i = i + 1;
                                perline = arr[i];
                                list.Add(perline + "\n");
                            } while (perline.Contains("GAMMA_TUNING") == false);
                            string[] gamma_set_lines = (string[])list.ToArray(typeof(string));
                            Text_Processing(gamma_set_lines);
                            try
                            {
                                string pathsave_file = System.Environment.CurrentDirectory + "\\l_gamma";
                                if (Directory.Exists(pathsave_file) == false)
                                {
                                    Directory.CreateDirectory(pathsave_file);
                                }
                                string pathload = System.Environment.CurrentDirectory + "\\l_gamma" + "\\gamma" + Multi_gamma_group_index.ToString() + ".csv";
                                Multi_LoadGamma(pathload);
                                Application.DoEvents();

                            }
                            catch {; }
                            if (Total_points > 15)
                            {
                                //  rbt_VT6110_P25.Checked = true;
                            }
                            else
                            {
                                // rbt_VT6110_IRC.Checked = true;
                            }
                            btnAutoGamma_NT38360_Click(null, null); //替换成相应IC的自动gamma启动函数名
                            list_view_index = i;
                            break;
                        }
                        else if (perline.Contains("GAMMA_CAL"))
                        {
                            perline = perline.Replace("GAMMA_CAL", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            //VT6110_Gamma_Group.Value = Multi_gamma_group_index - 1;
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_NT38360_Normal.Checked = true;
                            NT38360_Inter_Gamma_Click(null, null);

                            list_view_index = i;
                        }
                        else if (perline.Contains("UDC_CAL"))
                        {
                            perline = perline.Replace("UDC_CAL", "");
                            perline = perline.Replace("_", "");
                            Multi_gamma_group_index = Convert.ToInt32(perline);
                            Gamma_Band_Select.Text = Multi_gamma_group_index.ToString();
                            rBt_NT38360_UDC.Checked = true;
                            NT38360_Inter_Gamma_Click(null, null);

                            list_view_index = i;
                        }

                        else if (perline.Contains("EXITGAMMA"))
                        {
                            NT38360_Exit_Gamma_Click(null, null);
                            i = i + 1;
                        }
                        else
                        {
                            i = i + 1;
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("输入格式有误！");
                }
            }
        }
        private void Multi_SaveGamma(string path)
        {
            try
            {
                string str = "保存Gamma文件";
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(path, false, Encoding.UTF8);
                str = str + "R0," + NT38360_R0.Text + "," + "G0," + NT38360_G0.Text + "," + "B0," + NT38360_B0.Text + "\r\n";
                str = str + "R1," + NT38360_R1.Text + "," + "G1," + NT38360_G1.Text + "," + "B1," + NT38360_B1.Text + "\r\n";
                str = str + "R2," + NT38360_R2.Text + "," + "G2," + NT38360_G2.Text + "," + "B2," + NT38360_B2.Text + "\r\n";
                str = str + "R3," + NT38360_R3.Text + "," + "G3," + NT38360_G3.Text + "," + "B3," + NT38360_B3.Text + "\r\n";
                str = str + "R4," + NT38360_R4.Text + "," + "G4," + NT38360_G4.Text + "," + "B4," + NT38360_B4.Text + "\r\n";
                str = str + "R5," + NT38360_R5.Text + "," + "G5," + NT38360_G5.Text + "," + "B5," + NT38360_B5.Text + "\r\n";
                str = str + "R6," + NT38360_R6.Text + "," + "G6," + NT38360_G6.Text + "," + "B6," + NT38360_B6.Text + "\r\n";
                str = str + "R7," + NT38360_R7.Text + "," + "G7," + NT38360_G7.Text + "," + "B7," + NT38360_B7.Text + "\r\n";
                str = str + "R8," + NT38360_R8.Text + "," + "G8," + NT38360_G8.Text + "," + "B8," + NT38360_B8.Text + "\r\n";
                str = str + "R9," + NT38360_R9.Text + "," + "G9," + NT38360_G9.Text + "," + "B9," + NT38360_B9.Text + "\r\n";
                str = str + "R10," + NT38360_R10.Text + "," + "G10," + NT38360_G10.Text + "," + "B10," + NT38360_B10.Text + "\r\n";
                str = str + "R11," + NT38360_R11.Text + "," + "G11," + NT38360_G11.Text + "," + "B11," + NT38360_B11.Text + "\r\n";
                str = str + "R12," + NT38360_R12.Text + "," + "G12," + NT38360_G12.Text + "," + "B12," + NT38360_B12.Text + "\r\n";
                str = str + "R13," + NT38360_R13.Text + "," + "G13," + NT38360_G13.Text + "," + "B13," + NT38360_B13.Text + "\r\n";
                str = str + "R14," + NT38360_R14.Text + "," + "G14," + NT38360_G14.Text + "," + "B14," + NT38360_B14.Text + "\r\n";
                str = str + "R15," + NT38360_R15.Text + "," + "G15," + NT38360_G15.Text + "," + "B15," + NT38360_B15.Text + "\r\n";
                str = str + "R16," + NT38360_R16.Text + "," + "G16," + NT38360_G16.Text + "," + "B16," + NT38360_B16.Text + "\r\n";
                str = str + "R17," + NT38360_R17.Text + "," + "G17," + NT38360_G17.Text + "," + "B17," + NT38360_B17.Text + "\r\n";
                str = str + "R18," + NT38360_R18.Text + "," + "G18," + NT38360_G18.Text + "," + "B18," + NT38360_B18.Text + "\r\n";
                str = str + "R19," + NT38360_R19.Text + "," + "G19," + NT38360_G19.Text + "," + "B19," + NT38360_B19.Text + "\r\n";
                str = str + "R20," + NT38360_R20.Text + "," + "G20," + NT38360_G20.Text + "," + "B20," + NT38360_B20.Text + "\r\n";
                str = str + "R21," + NT38360_R21.Text + "," + "G21," + NT38360_G21.Text + "," + "B21," + NT38360_B21.Text + "\r\n";
                str = str + "R22," + NT38360_R22.Text + "," + "G22," + NT38360_G22.Text + "," + "B22," + NT38360_B22.Text + "\r\n";
                str = str + "R23," + NT38360_R23.Text + "," + "G23," + NT38360_G23.Text + "," + "B23," + NT38360_B23.Text + "\r\n";
                str = str + "R24," + NT38360_R24.Text + "," + "G24," + NT38360_G24.Text + "," + "B24," + NT38360_B24.Text + "\r\n";
                sw.WriteLine(str);
                sw.Dispose(); //释放资源
                sw.Close(); //关闭数据流
            }
            catch
            {
                MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
            }
        } //
        private void Text_Processing(string[] txt_path)
        {
            double L_X, L_Y, L_Z;
            string[] arr = txt_path;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            try  //获取调整spec
            {
                txtNT38360_lumoff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txtNT38360_Xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txtNT38360_Yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];

                txtNT38360_Gamma.Text = Diven_Gamma[0].Curve.ToString();
                txtNT38360_lum.Text = Diven_Gamma[0].Lum.ToString();
                txtNT38360_X.Text = Diven_Gamma[0].X.ToString();
                txtNT38360_Y.Text = Diven_Gamma[0].Y.ToString();

                for (int i = 0; i < Total_points; i++)
                {   //Diven_Offset[p].NO 灰阶绑点                 
                    D_lum[i] = (Math.Pow((Diven_Offset[i].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    CaSDKObj.Lvxy_to_XYZ(D_lum[i], Diven_Gamma[0].X, Diven_Gamma[0].Y, out L_X, out L_Y, out L_Z);//X/Z/Z转换
                    D_lums[i, 0] = L_X; //R/B/G分亮度计算
                    D_lums[i, 1] = L_Y;
                    D_lums[i, 2] = L_Z;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }
        //Gamma自动载入
        private void Multi_LoadGamma(string path)
        {
            string strtmp = "";
            //读取文本信息
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string strLine = streamReader.ReadLine();
            //循环读取文本信息
            while (!string.IsNullOrEmpty(strLine))
            {
                strtmp = strtmp + strLine + ",";
                strLine = streamReader.ReadLine(); //循环读取下一行
            }
            fs.Close(); //关闭流
            streamReader.Close();
            string[] arrtmp = strtmp.Split(new Char[] { ',' });
            NT38360_R0.Text = arrtmp[1].ToString(); NT38360_G0.Text = arrtmp[3].ToString(); NT38360_B0.Text = arrtmp[5].ToString();
            NT38360_R1.Text = arrtmp[7].ToString(); NT38360_G1.Text = arrtmp[9].ToString(); NT38360_B1.Text = arrtmp[11].ToString();
            NT38360_R2.Text = arrtmp[13].ToString(); NT38360_G2.Text = arrtmp[15].ToString(); NT38360_B2.Text = arrtmp[17].ToString();
            NT38360_R3.Text = arrtmp[19].ToString(); NT38360_G3.Text = arrtmp[21].ToString(); NT38360_B3.Text = arrtmp[23].ToString();
            NT38360_R4.Text = arrtmp[25].ToString(); NT38360_G4.Text = arrtmp[27].ToString(); NT38360_B4.Text = arrtmp[29].ToString();
            NT38360_R5.Text = arrtmp[31].ToString(); NT38360_G5.Text = arrtmp[33].ToString(); NT38360_B5.Text = arrtmp[35].ToString();
            NT38360_R6.Text = arrtmp[37].ToString(); NT38360_G6.Text = arrtmp[39].ToString(); NT38360_B6.Text = arrtmp[41].ToString();
            NT38360_R7.Text = arrtmp[43].ToString(); NT38360_G7.Text = arrtmp[45].ToString(); NT38360_B7.Text = arrtmp[47].ToString();
            NT38360_R8.Text = arrtmp[49].ToString(); NT38360_G8.Text = arrtmp[51].ToString(); NT38360_B8.Text = arrtmp[53].ToString();
            NT38360_R9.Text = arrtmp[55].ToString(); NT38360_G9.Text = arrtmp[57].ToString(); NT38360_B9.Text = arrtmp[59].ToString();
            NT38360_R10.Text = arrtmp[61].ToString(); NT38360_G10.Text = arrtmp[63].ToString(); NT38360_B10.Text = arrtmp[65].ToString();
            NT38360_R11.Text = arrtmp[67].ToString(); NT38360_G11.Text = arrtmp[69].ToString(); NT38360_B11.Text = arrtmp[71].ToString();
            NT38360_R12.Text = arrtmp[73].ToString(); NT38360_G12.Text = arrtmp[75].ToString(); NT38360_B12.Text = arrtmp[77].ToString();
            NT38360_R13.Text = arrtmp[79].ToString(); NT38360_G13.Text = arrtmp[81].ToString(); NT38360_B13.Text = arrtmp[83].ToString();
            NT38360_R14.Text = arrtmp[85].ToString(); NT38360_G14.Text = arrtmp[87].ToString(); NT38360_B14.Text = arrtmp[89].ToString();
            NT38360_R15.Text = arrtmp[91].ToString(); NT38360_G15.Text = arrtmp[93].ToString(); NT38360_B15.Text = arrtmp[95].ToString();
            NT38360_R16.Text = arrtmp[97].ToString(); NT38360_G16.Text = arrtmp[99].ToString(); NT38360_B16.Text = arrtmp[101].ToString();
            NT38360_R17.Text = arrtmp[103].ToString(); NT38360_G17.Text = arrtmp[105].ToString(); NT38360_B17.Text = arrtmp[107].ToString();
            NT38360_R18.Text = arrtmp[109].ToString(); NT38360_G18.Text = arrtmp[111].ToString(); NT38360_B18.Text = arrtmp[113].ToString();
            NT38360_R19.Text = arrtmp[115].ToString(); NT38360_G19.Text = arrtmp[117].ToString(); NT38360_B19.Text = arrtmp[119].ToString();
            NT38360_R20.Text = arrtmp[121].ToString(); NT38360_G20.Text = arrtmp[123].ToString(); NT38360_B20.Text = arrtmp[125].ToString();
            NT38360_R21.Text = arrtmp[127].ToString(); NT38360_G21.Text = arrtmp[129].ToString(); NT38360_B21.Text = arrtmp[131].ToString();
            NT38360_R22.Text = arrtmp[133].ToString(); NT38360_G22.Text = arrtmp[135].ToString(); NT38360_B22.Text = arrtmp[137].ToString();
            NT38360_R23.Text = arrtmp[139].ToString(); NT38360_G23.Text = arrtmp[141].ToString(); NT38360_B23.Text = arrtmp[143].ToString();
            NT38360_R24.Text = arrtmp[145].ToString(); NT38360_G24.Text = arrtmp[147].ToString(); NT38360_B24.Text = arrtmp[149].ToString();
        }
        private void NT38360_Inter_Gamma_Click(object sender, EventArgs e)
        {
            NT38360_Reg_Data.Text = "ff,f0";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "f9,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "50,04";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "ff,21";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "f9,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "33,03";
            NT38360_Others_Click(NT38360_RegW, null);
        }
        private void NT38360_Exit_Gamma_Click(object sender, EventArgs e)
        {
            NT38360_Reg_Data.Text = "ff,f0";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "f9,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "50,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "ff,21";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "f9,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "33,01";
            NT38360_Others_Click(NT38360_RegW, null);
        }


        private void button2_Click(object sender, EventArgs e)
        {
            NT38360_Bist_Pattern(nup_r.Value, nup_g.Value, nup_b.Value);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NT38360_Reg_Data.Text = "ff,f0";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "50,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "ff,25";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "ec,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "ff,21";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "2f,00";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
            NT38360_Reg_Data.Text = "33,01";
            NT38360_Others_Click(NT38360_RegW, null);
            Thread.Sleep(20);
        }

        private byte Gamma_node_index = 24;
        private void NT38360_G24_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            string str = numericUpDown.Name;
            string ch = str.Substring(str.Length - 2, 1);
            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] asc = ascii.GetBytes(ch);
            if (asc[0] < 48 || asc[0] > 57)
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 1, 1));
            }
            else
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 2, 2));
            }
        }

        private void NT38360_R24_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            string str = numericUpDown.Name;
            string ch = str.Substring(str.Length - 2, 1);
            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] asc = ascii.GetBytes(ch);
            if (asc[0] < 48 || asc[0] > 57)
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 1, 1));
            }
            else
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 2, 2));
            }
        }

        private void NT38360_B24_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown numericUpDown = (NumericUpDown)sender;
            string str = numericUpDown.Name;
            string ch = str.Substring(str.Length - 2, 1);
            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] asc = ascii.GetBytes(ch);
            if (asc[0] < 48 || asc[0] > 57)
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 1, 1));
            }
            else
            {
                Gamma_node_index = byte.Parse(str.Substring(str.Length - 2, 2));
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (NT38360_Gamma_SetBox.Text == string.Empty)
            {
                MessageBox.Show("内容为空!");
            }
            else
            {
                //创建保存对话框对象
                SaveFileDialog save = new SaveFileDialog();
                save.Filter = "gamma cfg files (*.txt)|*.txt"; //过滤文件类型
                save.FilterIndex = 1;
                save.RestoreDirectory = true;
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string path = save.FileName;
                    save.DefaultExt = ".txt";
                    StreamWriter sw = new StreamWriter(path);
                    sw.WriteLine(NT38360_Gamma_SetBox.Text);
                    sw.Close();
                }
            }
        }
        private void 载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "精度配置文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "gamma cfg write";

            if (of.ShowDialog() == DialogResult.OK)
            {
                NT38360_Gamma_SetBox.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    NT38360_Gamma_SetBox.AppendText(str_generate + "\r\n");
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }

    }
}
#endregion